package com.test5.liang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.generic.ARRAYLENGTH;
import com.test5.liang.pojo.User;
import com.test5.liang.pojo.registerLog;
import com.test5.liang.service.UserService;
import com.test5.liang.service.registerLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class adminController {

    @Autowired
    registerLogService registerLogService;

    @Autowired
    UserService userService;


    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 22:04 2021/12/12
     * @Param 查询登记信息
     * @return
     **/
    @RequestMapping("/registerMessage")
    public String registerMessage(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                                  @RequestParam("username")String username,Model model){


        PageHelper.startPage(pagenum, 8);  //pagenum：页数，pagesize:每页的信息数

        List<registerLog> registerLogs = registerLogService.selectAllRegisterLog();//调用业务层查询方法

        PageInfo<registerLog> pageInfo = new PageInfo<>(registerLogs); //得到分页结果对象

        model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息
        model.addAttribute("username", username);

        return "adminpart/Registermessage";
    }

    /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 21:33 2021/12/15
     * @Param
     * @return
     **/
    @RequestMapping("/adminInfo")
    public String adminInfo(@RequestParam("username")String username,Model model){

        User user = userService.findUserByName(username);
        model.addAttribute("user",user);
        System.out.println("****************");
        System.out.println(user.toString());
        String role;
        if (user.getUserRole()==1){
            role = "用户";
        }else {
            role = "管理员";
        }
        model.addAttribute("username",username);
        model.addAttribute("role",role);
        return "adminpart/adminInfo";
    }

     /**
     * @Author huangzhenliang
     * @Description //TODO 黄臻良
     * @Date 21:37 2021/12/15
     * @Param
     * @return
     **/
     @RequestMapping("/noRegisterMessage")
     public String noRegisterMessage(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
                                   @RequestParam("username")String username,Model model) throws ParseException {

         //设置一个装载未打卡的人
         ArrayList<User> list = new ArrayList<>();

         PageHelper.startPage(pagenum, 8);  //pagenum：页数，pagesize:每页的信息数

         List<User> users = userService.selectAllUser(); //调用业务层查询方法
         for (User user : users) {
             registerLog registerLog = registerLogService.selectTodayRegisterLogbyUserId(user.getUserId());
             if (registerLog==null){
                 //未登记次数
                 List<registerLog> registerLogs = registerLogService.selectAllRegisterLogByUserId(user.getUserId());
                 System.out.println("registerLogs"+registerLogs);
                 //总天数
                 Date date = new Date();
                 // 下面的yyyy-MM-dd是根据你自己提供的字符串来决定的，也可以改成yyyy/MM/dd
                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                 Date dateOne = dateFormat.parse("2021-12-1");
                 long days = Math.abs(((dateOne.getTime()-date.getTime())/(1000*60*60*24)));
                 System.out.println("相差的天数："+ days);

                 user.setLackdays(days-registerLogs.size());

                 //最近的一次打卡时间
                 String format = null;
                 if (registerLogs.size()!=0){
                    format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(registerLogs.get(0).getCreateTime());
                 }
                 user.setRecentday(format);
                 list.add(user);
             }
         }

         PageInfo<User> pageInfo = new PageInfo<>(list); //得到分页结果对象

         model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息
         model.addAttribute("username", username);

         return "adminpart/noRegistermessage";
     }

     /**
      * @Author huangzhenliang
      * @Description //TODO 黄臻良
      * @Date 23:32 2021/12/15
      * @Param
      * @return
      **/
     @RequestMapping("/usermanage")
     public String usermanage(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum,
             @RequestParam("username")String username,Model model){

         PageHelper.startPage(pagenum, 8);  //pagenum：页数，pagesize:每页的信息数

         List<User> users = userService.selectAllUser();//调用业务层查询方法

         PageInfo<User> pageInfo = new PageInfo<>(users); //得到分页结果对象

         model.addAttribute("pageInfo", pageInfo);  //携带分页结果信息
         model.addAttribute("username", username);

         return "adminpart/usermanage";
     }





}